package bjut.core.config;

import bjut.core.netty.ZRpcNettyServer;
import bjut.core.utils.CuratorUtil;
import bjut.core.utils.ThreadPoolUtil;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @Author: zhuzhiming
 * @Date: 2022/4/16 20:00
 */
public class ShutDownHook {

    public static void clearAll() {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), ZRpcNettyServer.PORT);
                CuratorUtil.clearServices(CuratorUtil.getZkClient(), inetSocketAddress);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            ThreadPoolUtil.shutdownAllThreadPool();
        }));
    }

}
